package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.TeachComplete;
import me.zhengjie.modules.system.domain.TeskAndstudent;
import me.zhengjie.modules.system.domain.TeskExerciseSetComplete;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.TeachCompleteService;
import me.zhengjie.modules.system.service.TeskAndstudentService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "教材完成")
@RequestMapping("/api/teachComplete")
public class TeachCompleteController {

    private final TeachCompleteService techCompleteService;

    private final UserService userService;

    private final TeskAndstudentService teskAndstudentService;

    @PostMapping
    @Log("教材完成")
    @ApiOperation("教材完成任务")
    public ResponseEntity<Object> teachComplete(@RequestBody TeachComplete teskExerciseSetComplete) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teakId",teskExerciseSetComplete.getTeskId());
        TeachComplete currentteskExerciseSetComplete = techCompleteService.getOne(queryWrapper);

        User user = userService.findByName(SecurityUtils.getCurrentUsername());
        TeskAndstudent teskAndstudent = new TeskAndstudent();

        if(currentteskExerciseSetComplete.getId()!=null){
            teskExerciseSetComplete.setId(currentteskExerciseSetComplete.getId());
            techCompleteService.updateById(teskExerciseSetComplete);

            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("teskId", teskExerciseSetComplete.getTeskId());
            queryWrapper1.eq("userId", Math.toIntExact(user.getId()));
            teskAndstudentService.getOne(queryWrapper1);



            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("userId",user.getId());
            queryWrapper2.eq("teskId",teskExerciseSetComplete.getTeskId());
            TeskAndstudent  teskAndstudent1 = teskAndstudentService.getOne(queryWrapper2);
            teskAndstudent1.setState(teskExerciseSetComplete.getState());
            teskAndstudentService.updateTeskAndstudent(teskAndstudent1);
        } else {

            teskExerciseSetComplete.setUserId(Math.toIntExact(user.getId()));
            techCompleteService.save(teskExerciseSetComplete);

            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("userId",user.getId());
            queryWrapper2.eq("teskId",teskExerciseSetComplete.getTeskId());
            TeskAndstudent  teskAndstudent1 = teskAndstudentService.getOne(queryWrapper2);
            teskAndstudent1.setState(teskExerciseSetComplete.getState());
            teskAndstudentService.updateTeskAndstudent(teskAndstudent1);;
        }

        return ResponseEntity.ok("课程完成");
    }
}
